﻿using System;
using Pumpkin.Utils;

namespace Pumpkin.FrameBase
{
    public abstract class FrameModuleBase : IFrameModule
    {
        public Trigger<string, object, object> Trigger
        {
            get
            {
                return trigger;
            }
        }

        private Trigger<string, object, object> trigger = new Trigger<string, object, object>();

        public virtual void OnLoaded()
        {
            FrameModuleMgr.Instance.InitNext();
        }

        public abstract void Init();
        public abstract string GetName();

        public bool HasModuleEvent(string eventName)
        {
            return trigger.HasTriggerEvent(eventName);
        }

        public void RegistModuleEvent(string eventName, Action<object, object> handler)
        {
            trigger.AddTriggerEvent(eventName, handler);
        }

        public void TriggerModuleEvent(string eventName, object o1, object o2)
        {
            trigger.InvokeTrigger(eventName, o1, o2);
        }

        public void RemoveModuleEvent(string eventName)
        {
            trigger.RemoveTrigger(eventName);
        }

        public void RemoveAllEvents()
        {
            trigger.RemoveAllTriggers();
        }

        public virtual void Dispose()
        {
            var msg = string.Format("Module[{0}] cant be disposed", GetName());
            throw new InvalidOperationException(msg);
        }
    }
}
